<?php
/**
 * @package Component Metromap for Joomla! 1.5
 * @version $Id: metromapline.php 4 2010-08-17 13:58:48Z batyrkin $
 * @author Batyrkin Anton
 * @copyright (C) 2010- Batyrkin Anton
 * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 **/

defined( '_JEXEC' ) or die();

class MetromapControllerMetroMapLine extends MetromapController
{
	function __construct()
	{
		parent::__construct();

		// Register Extra tasks
		$this->registerTask( 'add'  , 	'edit' );
		$this->registerTask( 'apply'  , 'save' );
	}

	function edit()
	{
		JRequest::setVar( 'view', 'metromapline' );
		JRequest::setVar( 'layout', 'form'  );
		JRequest::setVar('hidemainmenu', 1);

		parent::display();

		$model = $this->getModel( 'metromapline' );
		$model->checkout();
	}

	function save() {
		$post					= JRequest::get('post');
		$cid					= JRequest::getVar( 'cid', array(0), 'post', 'array' );
		$post['id'] 			= (int) $cid[0];

		$model = $this->getModel( 'metromapline' );

		switch ( JRequest::getCmd('task') ) {
			case 'apply':
				$id	= $model->store($post);
				if ($id && $id > 0) {
					$msg = JText::_( 'Changes to Line Saved' );
					//$id		= $model->store($post);
				} else {
					$msg = JText::_( 'Error Saving Line' );
					$id		= $post['id'];
				}
				$this->setRedirect( 'index.php?option=com_metromap&controller=metromapline&task=edit&cid[]='. $id, $msg );
				break;

			case 'save':
			default:
				if ($model->store($post)) {
					$msg = JText::_( 'Line Saved' );
				} else {
					$msg = JText::_( 'Error Saving Line' );
				}
				$this->setRedirect( 'index.php?option=com_metromap&view=metromaplines', $msg );
				break;
		}

		$model->checkin();
	}

	function remove() {
		global $mainframe;
		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to delete' ) );
		}


		$model = $this->getModel( 'metromapline' );

		if(!$model->delete($cid)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
			$msg = JText::_( 'Error Deleting Line' );
		} else {
			$msg = JText::_( 'Line Deleted' );
		}

		$link = 'index.php?option=com_metromap&view=metromaplines';
		$this->setRedirect( $link, $msg );
	}

	function publish() {
		global $mainframe;

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to publish' ) );
		}

		$model = $this->getModel('metromapline');

		if(!$model->publish($cid, 1)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		$link = 'index.php?option=com_metromap&view=metromaplines';
		$this->setRedirect($link);
	}

	function unpublish() {
		global $mainframe;

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to unpublish' ) );
		}

		$model = $this->getModel('metromapline');
		if(!$model->publish($cid, 0)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		$link = 'index.php?option=com_metromap&view=metromaplines';
		$this->setRedirect($link);
	}

	function cancel() {
		$model = $this->getModel( 'metromapline' );
		$model->checkin();

		$msg = JText::_( 'Operation Cancelled' );
		$link = 'index.php?option=com_metromap&view=metromaplines';
		$this->setRedirect( $link, $msg );
	}

	function orderup() {
		$model = $this->getModel( 'metromapline' );
		$model->move(-1);

		$link = 'index.php?option=com_metromap&view=metromaplines';
		$this->setRedirect( $link );
	}

	function orderdown() {
		$model = $this->getModel( 'metromapline' );
		$model->move(1);

		$link = 'index.php?option=com_metromap&view=metromaplines';
		$this->setRedirect( $link );
	}

	function saveorder() {
		$cid 	= JRequest::getVar( 'cid', array(), 'post', 'array' );
		$order 	= JRequest::getVar( 'order', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);
		JArrayHelper::toInteger($order);

		$model = $this->getModel( 'metromapline' );
		$model->saveorder($cid, $order);

		$msg = JText::_( 'New ordering saved' );
		$link = 'index.php?option=com_metromap&view=metromaplines';
		$this->setRedirect( $link, $msg  );
	}
}